package com.ziytek.web.citizen.service.impl;

import com.ziytek.web.citizen.configuration.db.DDS;
import com.ziytek.web.citizen.constant.DdsConstant;
import com.ziytek.web.citizen.constant.NumConstant;
import com.ziytek.web.citizen.constant.SettingConstant;
import com.ziytek.web.citizen.mapper.BasicCabinetIpMapper;
import com.ziytek.web.citizen.model.req.IpSetReq;
import com.ziytek.web.citizen.pojo.basic.BasicCabinetIp;
import com.ziytek.web.citizen.service.itf.CabinetIpService;
import com.ziytek.web.citizen.util.CabinetNoUtil;
import com.ziytek.web.citizen.util.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Objects;

/**
 * @author fenghx
 * @version 1.0
 * @className CabinetIpServiceImpl
 * Description:
 * @date 2024/3/13 10:54
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CabinetIpServiceImpl implements CabinetIpService {
    private final BasicCabinetIpMapper cabinetIpMapper;

    @Override
    @DDS(DdsConstant.DS_BASIC)
    public int saveAndUpdate(IpSetReq req) {
        var success = NumConstant.INT_ZERO;
        if (req == null || CollectionUtils.isEmpty(req.getIpList())) {
            return success;
        }
        var ipSettings = req.getIpList();

        for (var setting : ipSettings) {
            if (Objects.isNull(setting.getIp()) || Objects.isNull(setting.getCabinetNo())) {
                log.error("机柜网络IP地址信息保存失败机柜编号及iP不能为空");
                continue;
            }

            try {
                var cabinetNo = CabinetNoUtil.getCabinetNo(setting.getCabinetNo());
                var cabinetIpSet = queryCabinetIp(cabinetNo);
                if (cabinetIpSet == null) {
                    cabinetIpSet = new BasicCabinetIp();
                    cabinetIpSet.setCreateTime(System.currentTimeMillis());
                    cabinetIpSet.setCabinetNo(cabinetNo);
                    cabinetIpSet.setIp(setting.getIp());
                    cabinetIpSet.setAvailable(SettingConstant.IS_AVAILABLE);
                    cabinetIpSet.setUpdateTime(System.currentTimeMillis());
                    var id = cabinetIpMapper.queryMaxId();
                    cabinetIpSet.setId(id == null ? NumConstant.LONG_ONE : id);
                    cabinetIpMapper.insertCabinet(cabinetIpSet);
                } else {
                    cabinetIpSet.setCabinetNo(cabinetNo);
                    cabinetIpSet.setIp(setting.getIp());
                    cabinetIpSet.setAvailable(SettingConstant.IS_AVAILABLE);
                    cabinetIpSet.setUpdateTime(System.currentTimeMillis());
                    cabinetIpMapper.updateCabinet(cabinetNo, setting.getIp(), SettingConstant.IS_AVAILABLE, System.currentTimeMillis(), cabinetIpSet.getId());
                }
                success += NumConstant.INT_ONE;
            } catch (Exception e) {
                log.error("机柜网络IP地址信息保存失败:{}", setting.getCabinetNo(), e);
            }
        }

        return success;
    }

    @Override
    public BasicCabinetIp queryCabinetIp(String cabinetNo) {
        if (StringUtil.isEmpty(cabinetNo)) {
            return null;
        }

        return cabinetIpMapper.queryByCabinetNo(cabinetNo, SettingConstant.IS_AVAILABLE);
    }
}
